<!-- 初定级 -->
<template>

	<ListLayout>

		<Card>
			<template #head>
				初定级信息
			</template>
			<template #body>
				<evan-form :model="formData" ref="form">
					<evan-form-item label="项目级别" prop="projectLevel">
						<ItemRow v-model="formData.projectLevel" dicKey="itemProjectLevel"></ItemRow>
					</evan-form-item>
					<evan-form-item label="项目类型" prop="projectType">
						<ItemRow v-model="formData.projectType" dicKey="projectType"></ItemRow>
					</evan-form-item>
					<evan-form-item label="项目负责人" prop="principal">
						<ItemRow v-model="formData.principal" chooseType="select-user"></ItemRow>
					</evan-form-item>
					<evan-form-item label="项目优先级" prop="priority">
						<ItemRow ref="priority" v-model="formData.priority" dicKey="itemPriority"></ItemRow>
					</evan-form-item>

				</evan-form>
			</template>
		</Card>

		<Card v-show="[2,3].includes(formData.projectLevel * 1)">
			<template #head>
				其他信息
			</template>
			<template #body>
				<evan-form :model="otherFormData" ref="otherFormData">
					<evan-form-item label="业务答辩人" prop="businessRespondent">
						<ItemRow v-model="otherFormData.businessRespondent" chooseType="select-user"></ItemRow>
					</evan-form-item>
					<evan-form-item label="技术答辩人">
						<ItemRow v-model="otherFormData.technicalRespondent" chooseType="select-user-multiple"></ItemRow>
					</evan-form-item>
					<evan-form-item label="其他参与人">
						<ItemRow v-model="otherFormData.otherRespondent" chooseType="select-user-multiple"></ItemRow>
					</evan-form-item>
				</evan-form>
			</template>
		</Card>




		<template #footer>
			<HButtonGroup @leftClick="leftClick" @rightClick="rightClick">
				<template #left>
					取消
				</template>
				<template #right>
					提交
				</template>
			</HButtonGroup>
		</template>
	</ListLayout>

</template>
<script>
	import Card from '@/components/card/components/cardBaseWrapper.vue'
	import ListLayout from '@/components/layout/listLayout.vue'
	import HButtonGroup from '@/components/buttonGroup/groupTwo.vue'
	import ItemRow from '@/components/arrow/itemArrow.vue'
	import ProjectApi from '@/apis/project.js'
	import AttachmentCard from '@/components/card/attachment.vue'
	import PagesDemandRouterConfig from '@/pagesDemand/routerConfig.js'
	import { attachmentFormatter } from '@/common/submitFormatter.js'
	export default {
		components: {
			Card,
			ListLayout,
			HButtonGroup,
			ItemRow,
			AttachmentCard
		},

		data() {
			return {

				rules: {
					principal: [{ message: '请选择项目负责人', required: true, }],
					projectLevel: [{ message: '请选择项目级别', required: true, }],
					priority: [{ message: '请选择项目优先级', required: true, }],
					projectType: [{ message: '请选择项目类型', required: true, }],
					businessRespondent: [{
						message: '请选择业务答辩人',
						required: true,
					}],
				},
				formData: {
					itemCode: '',
					// 项目类型
					projectType: 1,
					// 项目负责人
					principal: '',
					// 项目优先级
					priority: '',
					// 项目级别
					projectLevel: 1,
					// 业务答辩人
					businessRespondent: '',
					// 技术答辩人
					technicalRespondent: '',
					// 其他答辩人
					otherRespondent: '',
				},
				otherFormData: {
					// 业务答辩人
					businessRespondent: '',
					// 技术答辩人
					technicalRespondent: '',
					// 其他答辩人
					otherRespondent: '',
				},

				routerParams: {
					itemCode: ''
				}

			}
		},
		onLoad(query) {
			let { itemCode } = query
			this.routerParams.itemCode = itemCode
			this.initData()

		},
		onReady() {
			this.$nextTick(() => {
				this.$refs.form.setRules(this.rules)
				this.$refs.otherFormData.setRules(this.rules)

			})
		},
		methods: {
			async initData() {
				let res = await ProjectApi.Demand.GetDemandDetail(this.routerParams.itemCode)
				let { firstGrading, demand } = res.body
				const data = firstGrading || {}

				this.$set(this.formData, 'itemCode', this.ItemCode);
				this.$set(this.formData, 'projectType', data?.projectType || 1);
				this.$set(this.formData, 'principal', data?.principal || null);
				this.$set(this.formData, 'priority', data?.priority || demand.level || null);
				this.$set(this.formData, 'projectLevel', data?.projectLevel || 1);
				this.$set(this.formData, 'businessRespondent', data?.businessRespondent || null);
				this.$set(this.formData, 'technicalRespondent', data?.technicalRespondent || null);
				this.$set(this.formData, 'otherRespondent', data?.otherRespondent || null);

			},
			delFormDataProposerList(index) {
				this.formDataProposerList.splice(index, 1)
			},

			leftClick() {
				wx.navigateBack()
			},
			rightClick() {
				let self = this;
				let is = true
				self.$refs.form.validate(async (valid) => {
					self.formData.itemCode = self.routerParams.itemCode
					let params = self.formData
					if (valid) {
						if ([2, 3].includes(self.formData.projectLevel * 1)) {
							self.$refs.otherFormData.validate((otherValid) => {
								Object.assign(params, self.otherFormData)
								is = otherValid
							})
						}
						if (is) {
							await ProjectApi.Demand.SubmitPreliminaryLevel(params);
							wx.redirectTo({
								url: PagesDemandRouterConfig.Demand
							})
						}

					}
				});
			},

		}
	}
</script>
